package com.uzai.console.web.profit;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.profit.ladderratiogroup.LadderRatioGroupQuery;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleIdDto;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleInfoDto;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleQuery;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleSetDefaultDto;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupQuery;
import com.uzai.console.dto.user.usertype.UserTypeQuery;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupIdDto;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupQuery;
import com.uzai.console.dto.wechatprivate.configplan.ConfigPlanUpdateDto;
import com.uzai.console.entity.LadderRatioGroup;
import com.uzai.console.entity.NuserRewardRuleGroup;
import com.uzai.console.entity.UserType;
import com.uzai.console.entity.UserTypeGroup;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.LadderRatioGroupMapper;
import com.uzai.console.mapper.NuserRewardRuleGroupMapper;
import com.uzai.console.mapper.UserTypeGroupMapper;
import com.uzai.console.mapper.UserTypeMapper;
import com.uzai.console.service.profit.NormalRewardRuleService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.base.UserTypeSelectKeyVo;
import com.uzai.console.vo.profit.normalrewardrule.NormalRewardRuleInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月25日 20:15
 */
@Api(value = "分佣规则",tags = "分佣管理|分佣规则")
@ApiSort(4)
@RestController
public class NormalRewardRuleContoller extends BaseController {
    private Logger logger = LoggerFactory.getLogger(NormalRewardRuleContoller.class);

    @Autowired
    private NormalRewardRuleService normalrewardruleService;
    @Autowired
    private UserTypeMapper userTypeMapper;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;
    @Autowired
    private NuserRewardRuleGroupMapper nuserRewardRuleGroupMapper;
    @Autowired
    private LadderRatioGroupMapper ladderRatioGroupMapper;

    @ApiOperation(value = "分页查询分佣规则列表", notes = "分页查询分佣规则列表", response = NormalRewardRuleInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/normalrewardrule/findNormalRewardRuleList")
    public Object findNormalRewardRuleList(@RequestBody NormalRewardRuleQuery normalrewardruleQuery){
        return normalrewardruleService.findNormalRewardRuleList(normalrewardruleQuery);
    }

    @ApiOperation(value = "查询分佣规则详情", notes = "查询分佣规则详情", response = NormalRewardRuleInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/normalrewardrule/findNormalRewardRuleInfo")
    public Object findNormalRewardRuleInfo(@RequestBody NormalRewardRuleIdDto normalrewardruleIdDto){
        return normalrewardruleService.findNormalRewardRuleInfo(normalrewardruleIdDto);
    }

    @SystemLogType(model = SystemModelEnum.NormalRewardRule, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加分佣规则", notes = "添加分佣规则")
    @ApiOperationSupport(order = 3)
    @PostMapping("/normalrewardrule/addNormalRewardRule")
    public Object addNormalRewardRule(@RequestBody NormalRewardRuleInfoDto normalrewardruleInfoDto){
        normalrewardruleService.addNormalRewardRule(normalrewardruleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.NormalRewardRule, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改分佣规则", notes = "修改分佣规则")
    @ApiOperationSupport(order = 4)
    @PostMapping("/normalrewardrule/updateNormalRewardRule")
    public Object updateNormalRewardRule(@RequestBody NormalRewardRuleInfoDto normalrewardruleInfoDto){
        normalrewardruleService.updateNormalRewardRule(normalrewardruleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.NormalRewardRule, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除分佣规则", notes = "删除会员类m型")
    @ApiOperationSupport(order = 5)
    @PostMapping("/normalrewardrule/deleteNormalRewardRule")
    public Object deleteNormalRewardRule(@RequestBody NormalRewardRuleIdDto normalrewardruleIdDto){
        normalrewardruleService.deleteNormalRewardRule(normalrewardruleIdDto);
        return true;
    }

    @ApiOperation(value = "根据会员类型组查找会员类型列表", notes = "根据会员类型组查找会员类型列表", response = UserTypeSelectKeyVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/normalrewardrule/findUserTypeSelectByUserTypeGroupId")
    public Object findUserTypeSelectByUserTypeGroupId(@RequestBody UserTypeGroupIdDto userTypeGroupIdDto){
        List<UserTypeSelectKeyVo> selectIdVoList = new ArrayList<>();
        UserTypeQuery userTypeQuery = new UserTypeQuery();
        userTypeQuery.setMerId(getLoginInfo().getMerId());
        userTypeQuery.setGroupId(userTypeGroupIdDto.getId());
        List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);
        if(userTypeList != null && userTypeList.size() > 0){
            for(UserType userType : userTypeList){
                UserTypeSelectKeyVo userTypeSelectKeyVo = new UserTypeSelectKeyVo();
                userTypeSelectKeyVo.setKey(String.valueOf(userType.getId()));
                userTypeSelectKeyVo.setValue(userType.getName());
                userTypeSelectKeyVo.setIdentity(userType.getIdentity());
                selectIdVoList.add(userTypeSelectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询杀熟规则组下拉列表", notes = "查询杀熟规则组下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 7)
    @PostMapping("/normalrewardrule/findNuserRewardRuleGroupSelect")
    public Object findNuserRewardRuleGroupSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        NuserRewardRuleGroupQuery nuserRewardRuleGroupQuery = new NuserRewardRuleGroupQuery();
        nuserRewardRuleGroupQuery.setMerId(getLoginInfo().getMerId());
        List<NuserRewardRuleGroup> nuserRewardRuleGroupList = nuserRewardRuleGroupMapper.findByList(nuserRewardRuleGroupQuery);
        if(nuserRewardRuleGroupList != null && nuserRewardRuleGroupList.size() > 0){
            for(NuserRewardRuleGroup nuserRewardRuleGroup : nuserRewardRuleGroupList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(nuserRewardRuleGroup.getId()));
                selectKeyVo.setValue(nuserRewardRuleGroup.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询阶梯规则组下拉列表", notes = "查询阶梯规则组下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 8)
    @PostMapping("/normalrewardrule/findLadderRatioGroupSelect")
    public Object findLadderRatioGroupSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        LadderRatioGroupQuery ladderRatioGroupQuery = new LadderRatioGroupQuery();
        ladderRatioGroupQuery.setMerId(getLoginInfo().getMerId());
        List<LadderRatioGroup> ladderRatioGroupList = ladderRatioGroupMapper.findByList(ladderRatioGroupQuery);
        if(ladderRatioGroupList != null && ladderRatioGroupList.size() > 0){
            for(LadderRatioGroup ladderRatioGroup : ladderRatioGroupList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(ladderRatioGroup.getId()));
                selectKeyVo.setValue(ladderRatioGroup.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询会员类型组下拉列表", notes = "查询会员类型组下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 9)
    @PostMapping("/normalrewardrule/findUsertypeGroupSelect")
    public Object findUsertypeGroupSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        UserTypeGroupQuery userTypeGroupQuery = new UserTypeGroupQuery();
        userTypeGroupQuery.setMerId(getLoginInfo().getMerId());
        List<UserTypeGroup> userTypeGroupList = userTypeGroupMapper.findByList(userTypeGroupQuery);
        if(userTypeGroupList != null && userTypeGroupList.size() > 0){
            for(UserTypeGroup userTypeGroup : userTypeGroupList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(userTypeGroup.getId()));
                selectKeyVo.setValue(userTypeGroup.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }


    @SystemLogType(model = SystemModelEnum.NormalRewardRule, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "设置默认分佣规则", notes = "设置默认分佣规则")
    @ApiOperationSupport(order=10)
    @PostMapping("/normalrewardrule/setDefault")
    public Object setDefault(@RequestBody NormalRewardRuleSetDefaultDto normalRewardRuleSetDefaultDto){
        normalrewardruleService.setDefault(normalRewardRuleSetDefaultDto);
        return true;
    }

}
